﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class BaoCaoTheoNgay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                DateTime dt = Commomcs.convertDate(DateTime.Now.ToString("dd/MM/yyyy"));
                string ngay=dt.ToString("dd/MM/yyyyy");
                var source = new Microsoft.Reporting
                   .WebForms.ReportDataSource("BaoCaoNgay", new Baocao.Baocaotheongay().GetHoaDonxuat(dt,ngay));
                ReportViewer1.LocalReport.ReportPath = @"Admin\Baocao\BaoCaoNgay.rdlc";
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(source);
                ReportViewer1.LocalReport.Refresh();
                DateTime dt1=DateTime.Now;
                DateTime dts = Commomcs.convertDate(dt1.ToString("dd/MM/yyyy"));
                datepicker.Text=dts.ToString("dd/MM/yyyy");
                LoadMathang();
            }
        }
        public void LoadMathang()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            MATHANGRepository repository = new MATHANGRepository(new EFRepository<MATHANG>(), iunitfwork);
            IQueryable<MATHANG> objmathang = repository.All();
            Dlmathang.DataSource = objmathang;
            Dlmathang.DataValueField = "MAMH";
            Dlmathang.DataTextField = "TENMH";
            Dlmathang.DataBind();
            Dlmathang.Items.Insert(0, new ListItem { Text="---Chọn mặt hàng---",Value="0" });
        }
        protected void lbtnxem_Click(object sender, EventArgs e)
        {
            Microsoft.Reporting
                   .WebForms.ReportDataSource source;
            DateTime dt = DateTime.Parse(datepicker.Text);
            DateTime dts = new DateTime();
           string strMamh=Dlmathang.SelectedValue;
           if (Dlmathang.SelectedValue.Equals("0"))
           {
               DateTime dt1 = Commomcs.convertDate(DateTime.Now.ToString("dd/MM/yyyy"));
               string ngay = dt.ToString("dd/MM/yyyyy");
               source = new Microsoft.Reporting
                  .WebForms.ReportDataSource("BaoCaoNgay", new Baocao.Baocaotheongay().GetHoaDonxuat(dt1, ngay));
               ReportViewer1.LocalReport.ReportPath = @"Admin\Baocao\BaoCaoNgay.rdlc";
               ReportViewer1.LocalReport.DataSources.Clear();
               ReportViewer1.LocalReport.DataSources.Add(source);
               ReportViewer1.LocalReport.Refresh();
               DateTime dts1 = Commomcs.convertDate(dt1.ToString("dd/MM/yyyy"));
               datepicker.Text = dts1.ToString("dd/MM/yyyy");
             
           }


           if (!string.IsNullOrEmpty(datepicker.Text))
           {

               dts = Commomcs.convertDate(dt.ToString("dd/MM/yyyy"));
               string ngay = dts.ToString("dd/MM/yyyy");
             
               List<CHITIETHOADONXUAT> idint = dataprovider.Entity.CHITIETHOADONXUATs.Where(p => p.HOADONXUAT.NGAYXUAT == dts && p.MAMH == strMamh).ToList();
               List<int> lstid = new List<int>();
               foreach (CHITIETHOADONXUAT hdx in idint)
               {
                   if (!string.IsNullOrEmpty(hdx.SOLUONGVOVE.ToString()) && !string.IsNullOrEmpty(hdx.SOLUONGVOCUOC.ToString()))
                   {
                       lstid.Add((int)hdx.SOLUONGVOVE);
                   }
               }
               if (lstid.Count <= 0)
               {
                   source = new Microsoft.Reporting
               .WebForms.ReportDataSource("BaoCaoNgay", new Baocao.Baocaotheongay().GetHoaDonxuat(dts, ngay, strMamh));
                   ReportViewer1.LocalReport.ReportPath = @"Admin\Baocao\BaoCaoNgay.rdlc";
                   ReportViewer1.LocalReport.DataSources.Clear();
                   ReportViewer1.LocalReport.DataSources.Add(source);
                   ReportViewer1.LocalReport.Refresh();
               
               }
               if (lstid.Count > 0)
               {
                   source = new Microsoft.Reporting
              .WebForms.ReportDataSource("BaoCaoXuatTheoNgay", new Baocao.BaoCaoXuatTheoNgay().GetHoaDonxuat1(dt, ngay, strMamh));
                   ReportViewer1.LocalReport.ReportPath = @"Admin\Baocao\BaoCaoXuatTheoNgay.rdlc";
                   ReportViewer1.LocalReport.DataSources.Clear();
                   ReportViewer1.LocalReport.DataSources.Add(source);
                   ReportViewer1.LocalReport.Refresh();
               
               }
                  
           }
           }
                
        }
    
}